package Ptolemaiosz;

public class AvatarPhysicalEntity extends PhysicalEntity {

    Boolean jumping = false;
    
    public AvatarPhysicalEntity() {
        
        SkeletonLogger.logCall();
		
			
		SkeletonLogger.logReturn();
    }

	/**
	 * 
	 * @param direction
	 */
    
            
	public void move(Direction direction) {
		SkeletonLogger.logCall();
                
                //iranyelmozdulas vektorok
                Vector to_up = new Vector(0, 70);
                Vector to_right = new Vector(20, 0);
                Vector to_left = new Vector(-20, 0);
                
                //regi sebesseg koordinatak
                float oldvelX = this.getVelocity().getX();
                float oldvelY = this.getVelocity().getY();
                
                //iranybillentyu alapjan sebesseg hozzaadas
                if(direction == Direction.right && velocity.getX() < 20.0f){
                    this.addVelocity(to_right);
                    
                }
                
                if(direction == Direction.left  && velocity.getX() > -20.0f){
                    this.addVelocity(to_left);
                    
                }
                
                //amennyiben nincs ugras kozben
                if(direction == Direction.up){
                    
                    if(!jumping)
                    {
                        this.addVelocity(to_up);
                        jumping = true;
                    }
                    else
                        this.addVelocity(new Vector());
                    
                    
                }
                
                //uj sebesseg koordinatak
                float newvelX = this.getVelocity().getX();
                float newvelY = this.getVelocity().getY();
                //System.out.println("avatar RegiSebbessegvektora: (" + oldvelX + ", "+ oldvelY + "); UjSebbessegvektora: (" + newvelX + ", " + newvelY + ")");
                
		SkeletonLogger.logReturn();
           
    }
        
         public void setJumping(boolean b)
            {
                SkeletonLogger.logCall();
                jumping = b;
                SkeletonLogger.logReturn();

            }
         
         
         public void update(long deltaTime)
            {
                float deltaTimeinSec = deltaTime / 1000.0f;
                float gravity = Physics.getGravity();
                float horizontalAcceleration = gravity * 1.666666666666f;
                
                 //###############################
                 //##   MOVEMENTS & DELTA SPEEDS
                 //###############################
                
                        ////////////////////////////////////
                        // Calculate Horizontal Movement
                        ///////////////////////////////////

                            Float SpeedHorizontal = velocity.getX();
                            float movementHorizontal = 0.0f; //Alapbol nem mozog oldalra, csak ha van oldaliranyu sebessege
                            float newVelocityX = 0.0f; //Alapbol ha nincs oldaliranyu sebessege, akkor nem is lesz mas
             
                            //Csak akkor van barmi oldaliranyban ha van oldaliranyu szamolnivalo, ha van ilyen iranyu sebessege
                            if(SpeedHorizontal.compareTo(0.0f) != 0)
                                {

                                    //Ha balra menne, jobbra iranyu lesz a lassulas
                                    if(SpeedHorizontal.compareTo(0.0f) < 0)
                                        horizontalAcceleration = horizontalAcceleration * -1;


                                    //Csak addig mozogjon oldalra mig meg nem all, de ne billenjen at a mozgas  iranya
                                        Float timeToMoveHorizontal = Math.abs(SpeedHorizontal / horizontalAcceleration);
                                            timeToMoveHorizontal = Math.min(timeToMoveHorizontal, deltaTimeinSec);

                                    //Elmozdulas
                                        //S=v0*t - a/2*t^2
                                            movementHorizontal = (SpeedHorizontal * timeToMoveHorizontal) + ((horizontalAcceleration/2.0f) * (timeToMoveHorizontal * timeToMoveHorizontal));

                                    //Sebesseg megvaltozasa
                                            //v = a * t 
                                            newVelocityX = velocity.getX() + horizontalAcceleration * timeToMoveHorizontal;
                                }

                                    
                        /////////////////////////////////
                        // Calculate Vertical Movement
                        /////////////////////////////////

                            float SpeedVertical = velocity.getY();
                            
                            //Elmozdulas
                                //S=v0*t - a/2*t^2
                                    float movementVertical = (SpeedVertical * deltaTimeinSec) + ((gravity/2.0f) * (deltaTimeinSec * deltaTimeinSec));
                            
                            //Sebesseg megvaltozasa
                                    //v = a * t 
                                    float newVelocityY = velocity.getY() + gravity * deltaTimeinSec; 


                        /////////////////////////////////
                        // Apply Calculated movements & Speeds
                        /////////////////////////////////

                            Vector newPosition = new Vector();
                                newPosition.setX( position.getX() + movementHorizontal);
                                newPosition.setY( position.getY() + movementVertical);

                            this.setPosition(newPosition);
                            this.setVelocity(new Vector(newVelocityX, newVelocityY));
                         
                            
                            
                 //###############################
                 //##   CHECK IF EDGE REACHED
                 //###############################            
                            
                                             
                    //Mi tudjuk hogy az o logikai entitasa egy avatar
                    Avatar avatar = (Avatar) element;           
                            
                            
                            
                    /////////////////////////////////
                    // BOTTOM EDGE  
                    ///////////////////////////////// 
             
                        //Ha lefele kiesne a palyaszakaszbol
                        if(Float.compare(position.getY(),0.0f) < 0)
                            {
                                if(levelPartManager.matchChecker(Direction.down, avatar.getCurrentLevelPart()))
                                    {
                                        //aktualis levelpart beallitas
                                        LevelPart levelpart_down = levelPartManager.getNeighbourLevelPart(Direction.down, avatar.getCurrentLevelPart());
                                        avatar.setCurrentLevelPart(levelpart_down);

                                        position.setY(position.getY() + 100);  
                                    }
                                else    
                                    {
                                       levelPartManager.getLevel().getApplication().restartLevel();
                                    }
                            }
                        
                    /////////////////////////////////
                    // TOP EDGE  
                    ///////////////////////////////// 
             
                        //Ha felfele kiugran a palyaszakaszbol
                        if(Float.compare(position.getY(),100.0f) > 0)
                            {
                                if(levelPartManager.matchChecker(Direction.up, avatar.getCurrentLevelPart()))
                                    {
                                        //aktualis levelpart beallitas
                                        LevelPart levelpart_up = levelPartManager.getNeighbourLevelPart(Direction.up, avatar.getCurrentLevelPart());
                                        avatar.setCurrentLevelPart(levelpart_up);

                                        position.setY(position.getY() - 100);  
                                    }
                                else    
                                    {
                                       position.setY(100.0f);
                                       velocity.setY(0.0f);
                                    }
                            }
                        
                        
                    /////////////////////////////////
                    // RIGHT EDGE  
                    ///////////////////////////////// 
             
                        //Ha jobbra kimenne a palyaszakaszbol
                        if(Float.compare(position.getX(),100.0f) > 0)
                            {
                                if(levelPartManager.matchChecker(Direction.right, avatar.getCurrentLevelPart()))
                                    {
                                        //aktualis levelpart beallitas
                                        LevelPart levelpart_right = levelPartManager.getNeighbourLevelPart(Direction.right, avatar.getCurrentLevelPart());
                                        avatar.setCurrentLevelPart(levelpart_right);

                                        position.setX(position.getX() - 100);  
                                    }
                                else    
                                    {
                                       position.setX(100.0f);
                                       velocity.setX(0.0f);
                                    }
                            }     

             
                    /////////////////////////////////
                    // Left EDGE  
                    ///////////////////////////////// 
             
                        //Ha jobbra kimenne a palyaszakaszbol
                        if(Float.compare(position.getX(),0.0f) < 0)
                            {
                                if(levelPartManager.matchChecker(Direction.left, avatar.getCurrentLevelPart()))
                                    {
                                        //aktualis levelpart beallitas
                                        LevelPart levelpart_left = levelPartManager.getNeighbourLevelPart(Direction.left, avatar.getCurrentLevelPart());
                                        avatar.setCurrentLevelPart(levelpart_left);

                                        position.setX(position.getX() + 100);  
                                    }
                                else    
                                    {
                                       position.setX(0.0f);
                                       velocity.setX(0.0f);
                                    }
                            }
            }
}